//TODO: Refresh score screen

package com.servertest1;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ScoreScreen extends Activity implements OnItemClickListener,
		OnClickListener {
	
	private Button play;
	
	private TextView scoreP1R1, scoreP2R1, scoreP1R2, scoreP2R2, scoreP1R3, scoreP2R3, nameP1, nameP2;
	
	private Client client;
	
	private String[] scoreScreen;
	
	String[] board_bundle = new String[7];
	String opponent;
	
	private DatabaseHelper checker;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.scorescreen);
		
		Bundle extras = getIntent().getExtras();
		//gamePosition = extras.getInt("position");
		//your_turn_games = (ArrayList<Game>) extras.get("your_turn_games");
		scoreScreen = extras.getStringArray("score_screen_data");
		play = (Button) findViewById(R.id.playbutton);
		play.setOnClickListener(this);
		
		scoreP1R1 = (TextView) findViewById(R.id.textp1r1);
		scoreP2R1 = (TextView) findViewById(R.id.textp2r1);
		scoreP1R2 = (TextView) findViewById(R.id.textp1r2);
		scoreP2R2 = (TextView) findViewById(R.id.textp2r2);
		scoreP1R3 = (TextView) findViewById(R.id.textp1r3);
		scoreP2R3 = (TextView) findViewById(R.id.textp2r3);
		nameP1 = (TextView) findViewById(R.id.textp1);
		nameP2 = (TextView) findViewById(R.id.textp2);
		
		nameP1.setText(scoreScreen[0]);
		nameP2.setText(scoreScreen[1]);
		scoreP1R1.setText(scoreScreen[3]);
		scoreP2R1.setText(scoreScreen[4]);
		scoreP1R2.setText(scoreScreen[5]);
		scoreP2R2.setText(scoreScreen[6]);
		scoreP1R3.setText(scoreScreen[7]);
		scoreP2R3.setText(scoreScreen[8]);
		
		String whoseTurn = "";
		
		if(Integer.parseInt(scoreScreen[2]) == 0){
			whoseTurn = scoreScreen[1];
		}
		else if(Integer.parseInt(scoreScreen[2]) == 1){
			whoseTurn = scoreScreen[0];
		}
		else if(Integer.parseInt(scoreScreen[2]) == 2){
			whoseTurn = "O";
		}
		else{
			System.out.print("ERROR!!! whose turn variable is wrong!");
			finish();
		}
		
		checker = new DatabaseHelper(this);
		
		String yourself = "";
		if(!checker.isEmpty()) {
			yourself = checker.getUser();
		}
		else {
			System.out.println("ERROR! No user in phone database");
			finish();
		}
		checker.close();
		
		if(yourself.equals(whoseTurn)){
			play.setText("Play Game!");
		}
		else{
			play.setText("Go Back");
		}
		
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (data != null) {	
			System.out.println("IN HERE!!!!!!");
			// Retrieve game data from bundle
			Bundle extras = data.getExtras();
			String[] game_status = extras.getStringArray("game_finished");

			if(game_status[0].equals("board_updated")){
				play.setText("Go Back");
				if(game_status[1].equals("p1r1")){
					scoreP1R1.setText(game_status[2]);
				}
				else if(game_status[1].equals("p2r1")){
					scoreP2R1.setText(game_status[2]);
				}
				else if(game_status[1].equals("p1r2")){
					scoreP1R2.setText(game_status[2]);
				}
				else if(game_status[1].equals("p2r2")){
					scoreP2R2.setText(game_status[2]);
				}
				else if(game_status[1].equals("p1r3")){
					scoreP1R3.setText(game_status[2]);
				}
				else if(game_status[1].equals("p2r3")){
					scoreP2R3.setText(game_status[2]);
				}
				else {
					System.out.println("ERROR- INVALID TURN");
				}
			}
			else{
				System.out.println("ERROR- GAME STATUS");
				finish();
			}
		}
		else {		
			System.out.println("Intent data is null!!!");
		}
	}
	
	public void onClick(View v) {
		switch (v.getId())
		{
		case R.id.playbutton:
			if(play.getText().equals("Play Game!")){
				// Execute command on server
				String[] cmd = new String[2];
				cmd[0] = "get_game";
				cmd[1] = scoreScreen[9];
				
				new Get_Game().execute(cmd);
			}
			else if(play.getText().equals("Go Back")){
				String[] result = new String[1];
				result[0] = "update_board";
				Intent goBack = new Intent();
				goBack.putExtra("from_score_screen", result);
				setResult(RESULT_OK, goBack);
				finish();
			}
			else{
				System.out.println("ERROR Play button text");
				finish();
			}
			break;
		default:
			break;
		}

	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub

	}
	
	private class Get_Game extends AsyncTask<String, Void, String[]>
	{
		protected String[] doInBackground(String... cmd)
		{
			// Connect to server
			if (client == null)
			{
				client = new Client();
			}

			client.connect();
			client.sendData(cmd);
			String[] result = client.recieveData();
			client.close();

			return result;
		}

		protected void onPostExecute(String[] result)
		{	
			if (result[0].equals("game_retrieved"))
			{
				for (int i = 1; i < result.length; i++)
				{
					board_bundle[i-1] = result[i];
				}
				
				Intent myIntent = new Intent(ScoreScreen.this, Board.class);
				myIntent.putExtra("board_data", board_bundle);
				startActivityForResult(myIntent, 0);
			}
			else
			{
				System.out.println("Server's message not game_retrieved: " + result[0]);
			}
		}
	}
}
